package com.mapeditor.core 
{
	import AS3Events.AS3EventMessage;
	import AS3Events.AS3EventRouter;
	import com.mapeditor.math.Vector3;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	/**
	 * ...
	 * @author SE
	 */
	public class RoomFactory extends Sprite
	{
		private var _shapeSelection:Sprite;
		private var _selectionOffet:Vector3;
		private var _posOffset:Vector3;
		private var _startPos:Vector3;
		 
		private static var instance:RoomFactory;  
		
		function RoomFactory():void {
			_shapeSelection = new Sprite();
			_shapeSelection.mouseChildren = false;
			_shapeSelection.mouseEnabled = true;
			_shapeSelection.alpha = 0.5; 
			_shapeSelection.graphics.lineStyle(1,0xCCCCFF); 
			_shapeSelection.name = "shape_select";
			_shapeSelection.buttonMode = true;
			_posOffset = new Vector3();
			_startPos = new Vector3();
				
		}
		
		public static function getInstance():RoomFactory
		{
			if (instance == null)  
			{
                instance = new RoomFactory();  				
			}
            return instance;  
		}
		
		public function selectionStart(e:MouseEvent):void {
			
			if (e.target.name != "shape_select")
			{
				
				stage.addEventListener(MouseEvent.MOUSE_MOVE, _selectionChange, false, 0, true);				
				stage.addEventListener(MouseEvent.MOUSE_UP, _selectionStop, false, 0, true);								
			
				_selectionOffet = new Vector3(mouseX, mouseY);
				
				clearSelect();
				if (!contains(_shapeSelection))
				{
					addChild(_shapeSelection);
				}
			}
		}
		public function clearSelect():void {
			_shapeSelection.x = 0;
			_shapeSelection.y = 0;									
			_shapeSelection.graphics.clear();
			
		}
		
		public function _selectionChange(e:MouseEvent):void {
			//trace(mouseX + "," + mouseY);
			
			var nw:Number;
			var nh:Number;
			var px:Number;
			var py:Number;
			
			_shapeSelection.graphics.clear();	
			
			_shapeSelection.graphics.lineStyle(1,0xCFCFFF);
			_shapeSelection.graphics.beginFill(0xCCCCFF, 0.5);
			_shapeSelection.graphics.drawRect(0, 0, mouseX - _selectionOffet.x, mouseY - _selectionOffet.y);
			_shapeSelection.x = _selectionOffet.x;
			_shapeSelection.y = _selectionOffet.y;
			_shapeSelection.graphics.endFill();
		}
		public function _selectionStop(e:MouseEvent):void {
			
			stage.removeEventListener(MouseEvent.MOUSE_MOVE, _selectionChange);				
			stage.removeEventListener(MouseEvent.MOUSE_UP, _selectionStop);
			//removeChild(_shapeSelection);
			var points:Array = new Array;
			points[0] = new Vector3;
			points[1] = new Vector3;
			
			
			
			if (_selectionOffet.x > mouseX)//selecionou da direita para esqqurda
			{
				points[0].x = mouseX;				
				points[1].x = _selectionOffet.x - (mouseX);
			}
			else
			{
				points[0].x = _selectionOffet.x;
				points[1].x = (mouseX ) - _selectionOffet.x;				
			}
			
			if (_selectionOffet.y > mouseY)//selecionou da baixo para cima
			{
				points[0].y = mouseY;
				points[1].y = _selectionOffet.y - (mouseY);
			}
			else
			{
				points[0].y = _selectionOffet.y;
				points[1].y = (mouseY) - _selectionOffet.y;				
			}
			//_shapeSelection.addEventListener(MouseEvent.MOUSE_DOWN, _selectionDown);
			AS3EventRouter.SendEvent("class_room", AS3EventMessage.CreateMessage("create_room", "", points));
			clearSelect();
		}
		
	}
	
}